/*
 * cache.h version 1.0
 *
 *  Created on: Feb 18, 2011
 *      Author: thienlong
 */

#ifndef CACHE_H_
#define CACHE_H_
#include "map_internal.h"
#include "mem_storage.h"
#include "stringbuffer.h"
#include <sys/queue.h>
#include "list.h"

typedef object (*item_loader)(object k, mem_storage *storage, void *args);
typedef struct node node;
struct node	{
	object k;
	object v;
	bool link;
	node *before;
	node *after;
};

#define NODE_INIT(nod, _k, _v, _link) { \
	(nod)->k = _k; \
	(nod)->v = _v; \
	(nod)->link = _link; \
}

typedef struct {
	pthread_mutex_t lock;
	item_loader loader;
	void *user_data;
	concurrentmap *data;
	node *order;
	int max_size;
	int size;
	int num_thread;
	int threshold;
	vector *locals;
	int max_local_size;
	array_cqueue *free_nodes;
#ifdef MEM_STORAGE_SUPPORT_THREAD_SAFE
	mem_storage *storage;
#endif
} lru_cache;

extern lru_cache *lru_cache_new(map_fn *fns, int max_size, int num_thread, item_loader loader, void *arg, int max_local_size
#ifdef MEM_STORAGE_SUPPORT_THREAD_SAFE
	, mem_storage *storage
#endif
);
extern object lru_cache_get(lru_cache *lru, object k, int thread_id);
extern object lru_cache_remove(lru_cache *lru, object k);
extern object lru_cache_update(lru_cache *lru, object k);
#endif /* CACHE_H_ */
